/* Developed by Vinh, Nguyen Hong <mansofta@gmail.com>
 * Copyright HIENDAI SOFTWARE COMPANY 2004
 * http://www.hiendai.com.vn
 * contact@hiendai.com.vn
*/
using System;
using System.IO;
using System.Text;
using System.Xml;

namespace MarcXmlParser {

	public class CSubfields
	{	
	    protected	XmlNode ptCurrentRootNode;		
		protected   XmlNodeList ptCurrentSubfieldList;	
		public CSubfields(XmlNode ptDatafieldNode)
		{
			ptCurrentRootNode=ptDatafieldNode;
			ptCurrentSubfieldList=ptCurrentRootNode.SelectNodes("./*[local-name() ='subfield']");  
		}
	public void Refresh()
		{
			ptCurrentSubfieldList=ptCurrentRootNode.SelectNodes("./*[local-name() ='subfield']");  
		}
	public int Count
		{
			get
			{
				//ptCurrentSubfieldList=ptCurrentRootNode.SelectNodes("./*[local-name() ='subfield']");  
				return ptCurrentSubfieldList.Count; 
			}	
		}
    public void Add(CSubfield Subfield)
		{	
			ptCurrentRootNode.AppendChild(ptCurrentRootNode.OwnerDocument.ImportNode(Subfield.RootNode,true));			  
			//ptCurrentSubfieldList=ptCurrentRootNode.SelectNodes("./*[local-name() ='subfield']");
		}
    public void Remove(int SubfieldIndex)
		{		
			ptCurrentRootNode.RemoveChild(ptCurrentSubfieldList.Item(SubfieldIndex));
			//ptCurrentSubfieldList=ptCurrentRootNode.SelectNodes("./*[local-name() ='subfield']");
		}
		//remove all empty subfield in the subfieldlist
	public void Clean()
		{					
			CSubfield ptMySubfield;
			ptMySubfield=new CSubfield();
			XmlNode ptSfNode;
			for(int i=0; i<ptCurrentSubfieldList.Count;i++)
			{
				ptSfNode=ptCurrentSubfieldList.Item(i);
				ptMySubfield.load_RootNode(ref ptSfNode);
				if(ptMySubfield.Value.Length==0)
				{
					ptCurrentRootNode.RemoveChild(ptCurrentSubfieldList.Item(i));
				}
			}
			ptCurrentSubfieldList=ptCurrentRootNode.SelectNodes("./*[local-name() ='subfield']");  
		}
    public CSubfield Subfield(int Index)
		{
				CSubfield ptMySubfield;
				ptMySubfield=new CSubfield();
				XmlNode ptSfNode;
				ptSfNode=ptCurrentSubfieldList.Item(Index);
				ptMySubfield.load_RootNode(ref ptSfNode); 
				return ptMySubfield;				
		}
        public bool get_Subfield(String sSfCode, ref CSubfield ptSubfield)
		{			
			XmlNode ptXnode;
	  	    StringBuilder QueryStr = new StringBuilder();
			QueryStr.Append("./*[local-name() ='subfield'][@code='");		
			QueryStr.Append(sSfCode);
			QueryStr.Append("']");
			ptXnode=ptCurrentRootNode.SelectSingleNode(QueryStr.ToString());
			if(ptXnode!=null)
			{
				ptSubfield.load_RootNode(ref ptXnode); 				
				return true;
			} else
			{
				return false;
			}
		}
        public CSubfield Subfield(String sSfCode)
		{			
			CSubfield ptSubfield;			
			ptSubfield=new CSubfield();
			XmlNode ptXnode;
	  	    StringBuilder QueryStr = new StringBuilder();
			QueryStr.Append("./*[local-name() ='subfield'][@code='");		
			QueryStr.Append(sSfCode);
			QueryStr.Append("']");
			ptXnode=ptCurrentRootNode.SelectSingleNode(QueryStr.ToString());
			if(ptXnode!=null)
			{				
				ptSubfield.load_RootNode(ref ptXnode); 				
			}
			return ptSubfield;
		}
        public void Remove(ref CSubfield ptSf)
        {
            XmlElement xElem = ptCurrentRootNode.OwnerDocument.DocumentElement;
            xElem.RemoveChild(ptSf.RootNode);            
        }
	};
}